package com.burgeon.backend.SHOPBACKEND.controller;

import com.burgeon.backend.SHOPBACKEND.entity.Article;
import com.burgeon.backend.SHOPBACKEND.entity.Media;
import com.burgeon.backend.SHOPBACKEND.entity.NewsItem;
import com.burgeon.backend.SHOPBACKEND.service.MessageNewsService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("WxNews")
@RestController
@CrossOrigin
public class MessageNewsController extends BaseController {
    @Autowired
    private MessageNewsService messageNewsService;

    /**
     * 获取图文列表
     * @param account
     * @return
     */
    @RequestMapping("getNews")
    public ResponseResult<ArrayList<NewsItem>> getNews( String account) throws UnsupportedEncodingException {
        ArrayList<NewsItem> data = messageNewsService.getNews(account);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 删除图文消息
     * @param account
     * @param media_id
     * @return
     */
    @RequestMapping("deleteNews")
    public ResponseResult<String> deleteNews( String account,String media_id) throws UnsupportedEncodingException {
        messageNewsService.deleteNews(account,media_id);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 修改图文消息
     * @param articles
     * @param account
     * @param media_id
     * @param index
     * @return
     */
    @RequestMapping("updateNews")
    public ResponseResult<String> updateNews(Article articles, String account,String media_id,String index) throws UnsupportedEncodingException {
         messageNewsService.updateNews(articles,account,media_id,index);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 上传图片,返回的是url，本接口所上传的图片不占用公众号的素材库中图片数量的100000个的限制。
     * 图片仅支持jpg/png格式，大小必须在1MB以下。
     * @param img
     * @param account
     * @return url
     */
    @RequestMapping("addImg")
    public ResponseResult<String> postImg(MultipartFile img, String account){
        String data = messageNewsService.postImg(img,account);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 上传永久素材图片，缩略图，返回media_id和url
     * @param img
     * @param account
     * @return
     */
    @RequestMapping("addMediaImg")
    public ResponseResult<Media> upMediaImg(MultipartFile img, String account){
        Media data = messageNewsService.addMaterial(img,account);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 上传图文素材，返回media_id
     * @param articles
     * @param account
     * @return
     */
    @RequestMapping("addMediaNews")
    public ResponseResult<String> upMediaNews(Article[] articles, String account) throws UnsupportedEncodingException {
        String data = messageNewsService.postNews(articles,account);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 发送图文消息
     * @param openIds
     * @param media_id
     * @param account
     * @return
     */
    @RequestMapping("sendNewsByOpenIds")
    public ResponseResult<String> sendNewsByOpenIds(List<String> openIds, String media_id, String account) throws UnsupportedEncodingException {
        String data = messageNewsService.sendNewsByOpenIds(openIds, media_id, account);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 发送全部图文消息
     * @param media_id
     * @param account
     * @return
     */
    @RequestMapping("sendNewsAll")
    public ResponseResult<String> sendNewsByOpenIds(String media_id, String account) throws UnsupportedEncodingException {
        String data = messageNewsService.sendNewsAll(media_id, account);
        return new ResponseResult<>(SUCCESS,data);
    }




}
